package com.ymatou.query.seller.trading.api.parameter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymatou.iapi.sellerquery.testcase.Ts_GetRefundBill;
import com.ymatou.iapi.trading.sellerquery.parameter.enums.SalesRefundStatusEnum;
import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.PostPayEnum;
import com.ymttest.database.model.YmtRefundBill;
import com.ymttest.database.model.Ymt_OrderPostPay;
import com.ymttest.database.model.Ymt_Order_Frozen;
import com.ymttest.utils.DataUtil;

public class Ymt_OrderExtension {
	/**
	 * 获取自动收货日期 用于性能优化
	 */
	public static String GetAutoReceiveTime(Ymt_Order_Frozen orderFrozen,
			int autoReceiveDelay) {
		return orderFrozen != null && !orderFrozen.isbPause()
				&& orderFrozen.getdFrozenTime() != null ? orderFrozen
				.getdFrozenTime() : null;
	}

	/**
	 * 订单实付金额（现金部分）
	 * 
	 * @return
	 */
	public static BigDecimal GetReceiveAmountOfCash(Ymt_OrderState orderState) {
		if (orderState == null)
			return null;
		BigDecimal amount = DataUtil.BigDecimall
				.GetValueOrDefault(orderState.getfPaidAmountOfCash())
				.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
						.getfPostPaidAmountOfCash()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(orderState
								.getfRefundedAmountOfCash()));
		return amount;
	}

	/**
	 * 获取支付的优惠券总金额
	 * 
	 * @param order
	 * @return
	 */
	public static BigDecimal GetTotalAmountOfCoupon(Ymt_Order order) {
		if (DataUtil.BigDecimall.GetValueOrDefault(order.getfYmtCouponAmount())
				.compareTo(BigDecimal.ZERO) > 0
				|| DataUtil.BigDecimall.GetValueOrDefault(
						order.getfSellerCouponAmount()).compareTo(
						BigDecimal.ZERO) > 0) {
			return DataUtil.BigDecimall.GetValueOrDefault(
					order.getfYmtCouponAmount()).add(
					DataUtil.BigDecimall.GetValueOrDefault(order
							.getfSellerCouponAmount()));
		}
		return DataUtil.BigDecimall.GetValueOrDefault(order.getCouponValue());
	}

	/**
	 * 订单付款（预付款）时，应付的现金金额
	 * 
	 * @return
	 */
	public static BigDecimal GetNeedPrePayAmountOfCash(Ymt_Order order) {
		// 订单应付金额 = 金额(M全款C定金,补款不再调整) + 调整金额 + 运费 - 红包抵扣金额 - 优惠券金额
		BigDecimal amount = DataUtil.BigDecimall
				.GetValueOrDefault(order.getfOrderPrice())
				.add(DataUtil.BigDecimall.GetValueOrDefault(order
						.getfOrderDiscount()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(order
						.getfDiscount()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(order.getfFreight()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(order
								.getfUseGiftAmount()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(order
								.getfSellerPromotionAmount())) // add
																// on
																// 2017-1-3
				.subtract(GetTotalAmountOfCoupon(order));
		return amount;
	}

	/**
	 * 获取收货红包金额
	 * 
	 * @return
	 */
	public static BigDecimal GetReceiveAmountOfGift(Ymt_OrderState orderState) {
		if (orderState != null) {
			BigDecimal amount = DataUtil.BigDecimall
					.GetValueOrDefault(orderState.getfPaidAmountOfGift())
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfPostPaidAmountOfGift()))
					.subtract(
							DataUtil.BigDecimall.GetValueOrDefault(orderState
									.getfRefundedAmountOfGift()));
			return amount;
		} else {
			return BigDecimal.ZERO;
		}
	}

	/**
	 * 获取实付金额
	 * 
	 * @return
	 */
	public static BigDecimal GetAmountOfRealPay(Ymt_Order order,
			Ymt_OrderState orderState) {
		if (orderState != null) {
			BigDecimal pay = GetPrePaidAmount(order, orderState);
			BigDecimal postpay = GetPostPaidAmount(orderState);
			// BigDecimal refund = GetRefundedAmount(orderState);
			// return pay.add(postpay).subtract(refund);
			return pay.add(postpay);
		} else {
			return BigDecimal.ZERO;
		}
	}

	/**
	 * 获取应付补款金额
	 * 
	 * @return
	 */
	public static BigDecimal GetNeedPostPayAmount(
			List<Ymt_OrderPostPay> postPayList) {
		if (DataUtil.isNullOrEmpty(postPayList) == false) {
			List<Ymt_OrderPostPay> postpays = postPayList
					.stream()
					.filter(x -> x.getiAction() == PostPayEnum.Applying
							.getValue()).collect(Collectors.toList());

			if (DataUtil.isNullOrEmpty(postpays)) {
				return BigDecimal.ZERO;
			}
			return postpays
					.get(postpays.size() - 1)
					.getfAmount()
					.subtract(
							postpays.get(postpays.size() - 1)
									.getfUseGiftAmount());
		} else {
			return BigDecimal.ZERO;
		}
	}

	/**
	 * 获取自动收货延迟日期天数
	 * 
	 * @param order
	 * @param orderInfoList
	 * @return
	 */
	public static int GetAutoReceiveDelay(Ymt_Order order,
			List<Ymt_OrderInfo> orderInfoList) {
		// todo dms 重构 取消 对 Catalog属性的依赖
		List<Ymt_OrderInfo> catalogStatus = orderInfoList.stream()
				.filter(x -> DataUtil.Stringg.isNullOrEmpty(x.getsProductId()))
				.collect(Collectors.toList());
		// 代购
		if (DataUtil.isNullOrEmpty(catalogStatus)) {
			return 20;
			// Int32.Parse(ConfigurationManager.AppSettings["AutoReceiveDaysForUs"]
			// ?? "20");
		}
		// 现货
		if (catalogStatus.stream().anyMatch(
				x -> x.getiCatalogStatus() == LogisticsTypeEnum.China
						.getValue())) {
			return 10;
			// return
			// Int32.Parse(ConfigurationManager.AppSettings["AutoReceiveDays"]
			// ?? "10");
		} else if (catalogStatus.stream().anyMatch(
				x -> x.getiCatalogStatus() == LogisticsTypeEnum.BondedDelivery
						.getValue())) {
			return 10;
			// return
			// Int32.Parse(ConfigurationManager.AppSettings["AutoReceiveDays"]
			// ?? "10");
		}
		// 直邮
		else {
			return 10;
			// return
			// Int32.Parse(ConfigurationManager.AppSettings["AutoReceiveDaysForUs"]
			// ?? "10");
		}
	}

	/**
	 * 获取配送方式
	 * 
	 * @return
	 */
	public static String GetDeliveryMethod(List<Ymt_OrderInfo> orderInfoList) {
		if (orderInfoList.stream().anyMatch(oi -> oi.getiCatalogStatus() == 3)) {
			return "护航直邮";
		} else if (orderInfoList.stream().allMatch(
				oi -> oi.getiCatalogStatus() == 2))

		{
			return "海外直邮";
		} else if (orderInfoList.stream().anyMatch(
				oi -> oi.getiCatalogStatus() == 4)) {
			return "保税发货";
		} else if (orderInfoList.stream().anyMatch(
				oi -> oi.getiCatalogStatus() == 1 && oi.getiCatalogType() == 1)) {
			return "国内转运";
		} else if (orderInfoList.stream().anyMatch(
				oi -> oi.getiCatalogStatus() == 1 && oi.getiCatalogType() == 0)) {
			return "国内现货";
		}
		return "";
	}

	public static int GetOrderType(Ymt_Order order,
			List<Ymt_OrderInfo> orderInfoList) {
		if (order.isbShangouOrder()) {
			return 1; // 1--扫货订单
		} else if (orderInfoList.stream().anyMatch(
				oi -> oi.getsCatalogId() == null)) {
			return 2; // 2--下单器订单
		} else {
			return 0; // 0--现货订单
		}
	}

	/**
	 * 获取订单类型
	 * 
	 * @param order
	 * @param orderInfoList
	 * @return
	 */
	public static int GetOrderType(Ymt_Order order, Ymt_OrderInfo orderInfo) {
		if (order.isbShangouOrder()) {
			return 1; // 1--扫货订单
		} else if (orderInfo.getsCatalogId() == null) {
			return 2; // 2--下单器订单
		} else {
			return 0; // 0--现货订单
		}
	}

	/**
	 * 获取首付款金额
	 * 
	 * @return
	 */
	public static BigDecimal GetPrePaidAmount(Ymt_Order order,
			Ymt_OrderState orderState) {
		if (order != null && order.getdPaidTime() != null && orderState != null) {
			BigDecimal amount = orderState
					.getfPaidAmountOfCash()
					.add(orderState.getfPaidAmountOfGift())
					.add(orderState.getfPaidAmountOfFreeCard())
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfDiscountOfThirdParty()))
					.add(orderState.getfPaidAmountOfYmtCoupon())
					.add(orderState.getfPaidAmountOfSellerCoupon());
			return amount;
		} else {
			return BigDecimal.ZERO;
		}
	}

	/**
	 * 获取补款金额
	 * 
	 * @return
	 */
	public static BigDecimal GetPostPaidAmount(Ymt_OrderState orderState) {
		if (null != orderState) {
			BigDecimal amount = DataUtil.BigDecimall
					.GetValueOrDefault(orderState.getfPostPaidAmountOfCash())
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfPostPaidAmountOfGift()))
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfPostPadiAmountOfCoupon()));
			return amount;
		}
		return BigDecimal.ZERO;
	}

	/**
	 * 获取订单中支付的现金金额
	 */
	public static BigDecimal GetCurrentAmountOfCash(Ymt_OrderState orderState) {
		BigDecimal amount = DataUtil.BigDecimall
				.GetValueOrDefault(orderState.getfPaidAmountOfCash())
				.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
						.getfPostPaidAmountOfCash()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(orderState
								.getfRefundedAmountOfCash()));
		return amount;
	}

	/**
	 * 获取订单当前金额
	 * 
	 * @return
	 */
	public static BigDecimal GetOrderCurrentPrice(Ymt_OrderState orderState) {
		BigDecimal part1 = orderState.getfPaidAmountOfCash();
		BigDecimal part2 = (orderState.getfPaidAmountOfYmtCoupon().compareTo(
				BigDecimal.ZERO) > 0 || orderState
				.getfPaidAmountOfSellerCoupon().compareTo(BigDecimal.ZERO) > 0)
				? orderState.getfPaidAmountOfYmtCoupon().add(
						orderState.getfPaidAmountOfSellerCoupon())
				: orderState.getfPaidAmountOfCoupon();
		BigDecimal part3 = orderState.getfPaidAmountOfGift();
		BigDecimal part4 = orderState.getfRefundedAmountOfCash();
		BigDecimal part5 = orderState.getfRefundedAmountOfCoupon();
		BigDecimal part6 = orderState.getfRefundedAmountOfGift();
		BigDecimal part7 = orderState.getfPostPaidAmountOfCash();
		BigDecimal part8 = orderState.getfPostPadiAmountOfCoupon();
		BigDecimal part9 = orderState.getfPostPaidAmountOfGift();
		BigDecimal part10 = orderState.getfPaidAmountOfFreeCard();
		// part11 .net新增，java还没加上该金额
		BigDecimal part11 = DataUtil.BigDecimall.GetValueOrDefault(orderState
				.getfDiscountOfThirdParty());
		BigDecimal amount = part1.add(part2).add(part3).subtract(part4)
				.subtract(part5).subtract(part6).add(part7).add(part8)
				.add(part9).add(part10).add(part11);
		return amount;
	}

	/**
	 * 获取退款金额
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static BigDecimal GetRefundedAmount(Ymt_Order order,
			Ymt_OrderState orderState) {
		// if (orderState != null) {
		// BigDecimal amount = DataUtil.BigDecimall
		// .GetValueOrDefault(orderState.getfRefundedAmountOfCash())
		// .add(DataUtil.BigDecimall.GetValueOrDefault(orderState
		// .getfRefundedAmountOfGift()))
		// .add(DataUtil.BigDecimall.GetValueOrDefault(orderState
		// .getfRefundedAmountOfCoupon()));
		// return amount;
		// }
		//
		// return BigDecimal.ZERO;
		if (orderState != null) {
			BigDecimal amount = DataUtil.BigDecimall
					.GetValueOrDefault(orderState.getfRefundedAmountOfCash())
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfRefundedAmountOfGift()))
					.add(DataUtil.BigDecimall.GetValueOrDefault(orderState
							.getfRefundedDiscountOfThirdParty()));
			BigDecimal refundCouponAmount = BigDecimal.ZERO;
			if (orderState.getfRefundedAmountOfCash()
					.compareTo(BigDecimal.ZERO) > 0) {
				List<Map> refundBills = Ts_GetRefundBill.execute(null,
						order.getiOrderId());
				if (!DataUtil.isNullOrEmpty(refundBills)) {
					for (Map refundBill : refundBills) {
						YmtRefundBill refundBillPo = new YmtRefundBill();
						DataUtil.mapToBean(refundBillPo, refundBill);
						int success = SalesRefundStatusEnum.Success.getCode();
						if (refundBillPo.getSalesRefundStatus() != null
								&& refundBillPo.getSalesRefundStatus() == success) {
							refundCouponAmount = orderState
									.getfRefundedAmountOfCoupon();
						}

					}
				}
			}
			return amount.add(refundCouponAmount);
		}
		return BigDecimal.ZERO;
	}
	/**
	 * 获取应付补款金额
	 * 
	 * @return
	 */
	public static BigDecimal GetNeedPostPayAmount(Ymt_Order order,
			List<Ymt_OrderPostPay> postPayList) {
		if (DataUtil.isNullOrEmpty(postPayList) == false) {
			List<Ymt_OrderPostPay> postpays = postPayList
					.stream()
					.filter(x -> x.getiAction() == PostPayEnum.Applying
							.getValue()).collect(Collectors.toList());

			if (DataUtil.isNullOrEmpty(postpays)) {
				return BigDecimal.ZERO;
			}
			return postpays
					.get(postpays.size() - 1)
					.getfAmount()
					.subtract(
							postpays.get(postpays.size() - 1)
									.getfUseGiftAmount());
		} else {
			return BigDecimal.ZERO;
		}
	}
}
